package castle;

import java.util.HashMap;
import java.util.Scanner;

public class Game {
    private Room currentRoom;
    private HashMap<String, Handler> handlers = new HashMap<>();
        
    public Game() 
    {
    	
    	handlers.put("help", new HandlerHelp(this));
    	handlers.put("go", new HandlerGo(this));
    	handlers.put("who", new HandlerWho(this));
    	handlers.put("search", new HandlerSearch(this));    	
    	handlers.put("pick", new HandlerPick(this));
    	handlers.put("check", new HandlerCheck(this));
    	handlers.put("bye", new HandlerBye(this));
        createRooms();
    }

    public String getHelpCmd() {
    	
    	return handlers.keySet().toString();
    	
    }
    
    public String getHelpExample() {
    	StringBuffer sb = new StringBuffer();
    	int i = 1;
    	for (Handler hd : handlers.values()) {
    		
    		if (!hd.getHelpExample().isEmpty()) {
    			sb.append("  " + (i++) + ") "+ hd.getHelpExample() + "\n");

    		}
    	}
    	return sb.toString();
    	
    }
    private void createRooms()
    {
        Room outside, lobby, pub, study, bedroom;
      
        //	制造房间
        outside = new Room("城堡外");
        lobby = new Room("大堂");
        pub = new Room("小酒吧");
        study = new Room("书房");
        bedroom = new Room("卧室");
        
        //	初始化房间的出口
        outside.setExit("east", lobby);
        outside.setExit("south", study);
        outside.setExit("west", pub);
        lobby.setExit("south", outside);
        pub.setExit("east", outside);
        study.setExit("north", outside);
        study.setExit("east", bedroom);
        bedroom.setExit("west", study);
        
        //  初始化房间陈设
        study.addSettings("一张陈旧的书桌，");
        bedroom.addSettings("一个衣柜");
        bedroom.addSettings("一张床");
        
        //  初始化房间中可携带的物品
        study.addItem("书桌上有一本书");
        study.addItem("抽屉里有一幅画");
        bedroom.addItem("衣柜里有一把钥匙");
        
        //初始化房间中的角色
        outside.addCharacter(new Character("令狐大侠", "主角"));
        pub.addCharacter(new Character("慕容公子", "路人"));
        pub.addCharacter(new Character("王姑娘", "路人"));


        currentRoom = outside;  //	从城堡门外开始
    }

    public Room getCurrentRoom() {
    	return currentRoom;
    }
    private void printWelcome() {
        System.out.println();
        System.out.println("欢迎来到城堡！");
        System.out.println("这是一个超级无聊的游戏。");
        System.out.println("如果需要帮助，请输入 'help' 。");
        System.out.println();

        showPrompt();

    }

    // 以下为用户命令



    public void goRoom(String direction) 
    {
        Room nextRoom = currentRoom.getExit(direction);
        Character me = currentRoom.getCharacters().get(0);

        if (nextRoom == null) {
            System.out.println("那里没有门！");
        }
        else {
        	nextRoom.getCharacters().add(0, me);
            currentRoom.getCharacters().remove(0);
            currentRoom = nextRoom;
            showPrompt();
        }
    }
	public void showPrompt() {
        System.out.println("现在你在" + currentRoom);
        if (!currentRoom.getSettings().isEmpty()) {
        	System.out.println("房间里有" + currentRoom.getSettings());
        }
        System.out.print("出口有：");
        System.out.println(currentRoom.getExistDesc());
	}
    
	public void play() {
		Scanner in = new Scanner(System.in);
        while ( true ) {
    		String line = in.nextLine();
    		String[] words = line.split(" ");
    		Handler handler  = handlers.get(words[0]);
    		String Value = "";
    		if (words.length > 1) {
    			Value = words[1];
    		}
    		if (handler != null) {
    			handler.doCmd(Value);
    			if (handler.isBye()) {
        			break;
    			}
    		
    		}

        }
        in.close();
	}
	public static void main(String[] args) {
		
		Game game = new Game();
		game.printWelcome();
		game.play();

        
        System.out.println("感谢您的光临。再见！");
        
	}

}
